package lincen.javase.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
 * 工具类
 * @author 	林岑
 * @time	2012年1月7日 09:55:08
 */
public class Util {

	public static final Random R = new Random();

	/**
	 * 将一个逗号分隔的字符串列表转换为整型数组<br>
	 * 前提intList格式合法.<br>
	 * 比如 1111,213,646,674987,16546,333<br>
	 * 	   
	 * 将用逗号分隔的int字符串, 转换成int数组<br>
	 * <br>
	 * 如果其中有空白, 那么那个值就等于 -1<br>
	 *  1111,213,646,,16546,333 	------>  1111,213,646,-1,16546,333
	 *  1111,213,646,,16546,333, 	------>  1111,213,646,-1,16546,333,-1
	 */
	public static int [] asArray(String intList) {

		if(intList == null || intList.equals("")){
			return new int[0];
		}

		intList = intList.replaceAll(",,", ",-1,");
		intList = intList.endsWith(",") ? intList += "-1" : intList;
		String [] idListString = intList.split( "," );
		int [] r = new int[idListString.length];
		for (int i = 0; i < r.length; i++) {
			if(idListString[i].equals("")) {
				r[i] = -1;
			} else {
				r[i] = Integer.parseInt(idListString[i].trim());
			}
		}
		return r;

	}
	
	/**
	 * 将一个逗号分隔的字符串列表转换为双精度数组<br>
	 * 前提intList格式合法.<br>
	 * 比如 1111,213,646,674987,16546,333<br>
	 * 	   
	 * 将用逗号分隔的int字符串, 转换成int数组<br>
	 * <br>
	 * 如果其中有空白, 那么那个值就等于 -1<br>
	 *  11.11,2.13,6.46,,16.546,333 	------>  11.11,2.13,6.46,-1.0,165.46,3.33
	 *  11.11,2.13,6.46,,16.546,333, 	------>  11.11,2.13,6.46,-1.0,165.46,3.33,-1.0
	 */
	public static double [] asDoubleArray(String intList) {

		if(intList == null || intList.equals("")){
			return new double[0];
		}

		intList = intList.replaceAll(",,", ",-1,");
		intList = intList.endsWith(",") ? intList += "-1" : intList;
		String [] idListString = intList.split( "," );
		double [] r = new double[idListString.length];
		for (int i = 0; i < r.length; i++) {
			if(idListString[i].equals("")) {
				r[i] = -1;
			} else {
				r[i] = Double.parseDouble(idListString[i].trim());
			}
		}
		return r;

	}

	/**
	 * 传入用逗号分隔的ID列表(String)  转成 List<Integer>
	 */
	public static List<Integer> asList(String idList) {

		List<Integer> idLis = new ArrayList<Integer>();
		if(idList == null || idList.equals("")) {
			return idLis;
		}

		String [] idListString = idList.split( "," );
		for (String id : idListString) {
			idLis.add( Integer.parseInt( id.trim() ));
		}
		return idLis;
	}

	/**
	 * 判断数组中所有元素是否相等
	 * @param suitNo
	 * @return
	 */
	public static boolean allIsTheSame(int... suitNo) {
		for (int i = 0; i < suitNo.length - 1; i++) {
			if(suitNo[i] != suitNo[i + 1]) {
				return false;
			}
		}
		return true;
	}

	public static boolean allIsTheSame(List<Integer> suitNo) {
		for (int i = 0; i < suitNo.size() - 1; i++) {
			if( !suitNo.get(i).equals(suitNo.get(i + 1)) ) {
				return false;
			}
		}
		return true;
	}
	

	/**
	 * 判断beCheck是不是在colection当中
	 * @param beCheck			被检查者
	 * @param collection			被检查的容器
	 * @return
	 */
	public static boolean isContainsIn(int beCheck, int[] collection) {
		for (long object : collection) {
			if(beCheck == object ) {
				return true;
			}
		}
		return false;
	}
	
	
	

	/**
	 * 获得指定个数的List
	 * @param winer 	
	 * @param winerNum
	 */
	public static<T> List<T> getRandomNumList(Collection<? extends T> list,
			int num) {
		Random r = new Random();
		List<T> l1 = new ArrayList<T>();
		for (T s : list) // 遍历Collection
			l1.add(s);
		if(l1.size() <= num) // 如果小于指定num 就取list
			return l1;
		List<T> l2 = new ArrayList<T>();
		for (int i = 0; i < num; i++){ // 从Collection中随机取num个
			int j = r.nextInt(l1.size() - i);
			l2.add(l1.get(j));
			l1.remove(j);
		}
		return l2;
	}
	
	//			public static void main(String[] args) {
	//				System.out.println(alter("-1,    123,    -1", 1, "xx"));
	//				for (int i = 0; i < 7; i++) {
	//					System.out.println(alter("-1,,3546543213,-1,3546546,-1", i, "xxxxxxx"));
	//				}
	//				for (int i = 0; i < 8; i++) {
	//					System.out.println(isOne((byte) 0x83, i));
	//				}
	//				System.out.println(Integer.toBinaryString(0x83));
	//			}

//	/**
//	 * 将容器中的元素过滤出来, 被选择的范围是start, end
//	 * @param applyList
//	 * @param start
//	 * @param end
//	 * @return
//	 */
//	@SuppressWarnings({ "rawtypes", "unchecked" })
//	public static Collection filter(Collection applyList, int start, int end) {
//		
//		Collection li = new HashSet();
//		int pos = 0;
//		for (Object object : applyList) {
//			if(pos >= start && pos <= end) {
//				li.add(object);
//			}
//		}
//		return li;
//	}
	
	/**
	 * 获得从右到左 第0位开始  第Index位是多少, 如果超过最大位数, 那么会返回0
	 * @param n
	 * @return
	 */
	public static int getNumber(int n, int index) {
		while(index-- > 0) {
			n /= 10;
		}
		return n % 10;
	}

	/**
	 * 计算多个数的和
	 * @param probs
	 * @return
	 */
	public static int sum(int... probs) {
		int sum = 0;
		for (int i : probs) {
			sum += i;
		}
		return sum;
	}
}
